<?php 
namespace App\Libs;

class FormatResult
{
	public $biz_type = '';
	public $code = '';
	public $message = '';
	public $biz_content = array();

	public function __construct($data=[])
	{
	    if(is_array($data)){
            foreach($data as $k => $v){
                if(isset($this->$k) && $k != 'biz_content'){
                    $this->$k = $v;
                }
            }
        }
	}

	public function getData()
	{
		$ret = get_object_vars($this);
		if(isset($ret['code'])){
			$ret['code'] = $ret['code'] . '';
		}
		foreach($ret['biz_content'] as $k => $v){
			if(is_string($v) || is_numeric($v) || is_float($v) || is_double($v)){
				$ret['biz_content'][$k] = $v . '';
			}
		}
		
		if(empty($ret['biz_content'])){
			$ret['biz_content'] = new \stdClass();
		}

		return $ret;
	}
	
	public function setError($code,$biz_content=[])
	{
	    $codemap = isset(FormatResultErrors::CODE_MAP[$code])? FormatResultErrors::CODE_MAP[$code] : FormatResultErrors::CODE_MAP['SYS.ERR'];
        $this->code = $codemap['code'];
        $this->message = $codemap['message'];

        $this->biz_content = $biz_content;
	}
	
	public function setSuccess($biz_content)
	{
	    $codemap = FormatResultErrors::CODE_MAP['SUCCESS'];
	    $this->code = $codemap['code'];
	    $this->message = $codemap['message'];
	    
	    $this->biz_content = $biz_content;
	}
}